package com.show.service.draw_time;

import com.show.util.QueryUtil;
import com.show.dao.draw_time.DrawTimeRepositoty;
import com.show.dto.draw_time.DrawTime;
import com.show.service.event.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

/**
 * DrawTimeService
 *
 * @author 姜文
 * @Param
 * @date 2018年3月9日, AM 08:59:04
 */
@Service
@Transactional
public class DrawTimeService {

    @Autowired
    private QueryUtil queryUtil;
    @Autowired
    private DrawTimeRepositoty drawTimeRepositoty;
    @Autowired
    private EventService eventService;

    /**
     * DrawTimeSave
     *
     * @author 姜文
     * @Param
     * @date 2018年3月9日, AM 08:59:04
     */
    public DrawTime save(String empId, String eventId, String orgId){
        DrawTime drawTime = new DrawTime();
        drawTime.setEmpId(empId.toString());
        drawTime.setEventId(eventId);
        drawTime.setEventLink(eventService.findOneForTypeId(eventId).getEventTypeId().toString());
        drawTime.setOrgId(orgId);
        drawTime.setSts("1");
        drawTime.setDrawTime(new Date());
        return drawTimeRepositoty.save(drawTime);
    }

    /**
     * DrawTimeUpdate
     *
     * @author 姜文
     * @Param
     * @date 2018年3月9日, AM 08:59:04
     */
    public DrawTime update(DrawTime drawTime){
        drawTime.setEndTime(new Date());
        return drawTimeRepositoty.saveAndFlush(drawTime);
    }

    /**
     * 查询当前环节数据信息
     *
     * @author 姜文
     * @Param
     * @date 2018年3月9日, AM 08:59:04
     */
    public DrawTime findDrawTimeByEmpIdAndEventId(String eventId, String empId){
        String sql = "SELECT * FROM `draw_time` WHERE event_id = '"+eventId+"' AND emp_id = '"+empId+"' AND end_time IS NULL AND sts = '1'";
        DrawTime drawTime = queryUtil.query(sql, DrawTime.class).get(0);
        return drawTime;
    }

    /**
     * 查询街道
     *
     * @author 姜文
     * @Param
     * @date 2018年3月9日, AM 08:59:04
     */
    public String findOrgIdByEventIdForJieDao(String eventId){
//        String sql = "SELECT org_id AS orgId FROM `draw_time` WHERE event_link = '22' AND event_id = '"+eventId+"' ORDER BY draw_time";
//        return queryUtil.query(sql, DrawTime.class).get(0).getOrgId();
        return drawTimeRepositoty.findByEventIdAndEventLinkOrderByDrawTime(eventId, "22").getOrgId();
    }

}
